package com.example.blog.aspect;

import com.example.blog.pojo.RequestInfoLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//@Aspect
//@Component
public class LogAspect {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Pointcut("execution(* com.example.blog.web.adminController.*.*(..))")
    public void log(){}
    @Before(value = "log()")
    public void Before(JoinPoint joinPoint){
        ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String url = request.getRequestURL().toString();
        String ip = request.getRemoteAddr();
        String classMethod = joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        RequestInfoLog requestInfoLog = new RequestInfoLog(url,ip,classMethod,args);
        logger.info("Request : {}",requestInfoLog);
    }
    @After(value = "log()")
    public void after(){
        System.out.println("----after-----");
    }
    @AfterReturning(returning = "result",pointcut = "log()")
    public void afterReturn(Object result){
        System.out.println("----afterReturn-----");
    }

}
